package com.hdjk.view.callback;

import com.hdjk.view.entity.HistoricalTransactionEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class HistoricalTransactionCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg=super.parseNetworkResponse(response, i);
        JSONObject object= (JSONObject) msg.getData();
        if (object!=null) {
            if (!object.isNull("data")) {
                JSONArray array = object.getJSONArray("data");
                List<HistoricalTransactionEntity> list = new ArrayList<>();
                if (array != null && array.length() > 0) {
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject jsonObject = array.getJSONObject(j);
                        HistoricalTransactionEntity entity = new HistoricalTransactionEntity();
                        if (!jsonObject.isNull("accountNumber")) {
                            entity.setAccountNumber(jsonObject.getString("accountNumber"));
                        }
                        if (!jsonObject.isNull("merchantNumber")) {
                            entity.setMerchantNumber(jsonObject.getString("merchantNumber"));
                        }
                        if (!jsonObject.isNull("fundCode")) {
                            entity.setFundCode(jsonObject.getString("fundCode"));
                        }
                        if (!jsonObject.isNull("fundName")) {
                            entity.setFundName(jsonObject.getString("fundName"));
                        }
                        if (!jsonObject.isNull("orderDate")) {
                            entity.setOrderDate(jsonObject.getString("orderDate"));
                        }
                        if (!jsonObject.isNull("portfolioId")) {
                            entity.setPortfolioId(jsonObject.getString("portfolioId"));
                        }
                        if (!jsonObject.isNull("rspId")) {
                            entity.setRspId(jsonObject.getString("rspId"));
                        }
                        if (!jsonObject.isNull("transactionAmount")) {
                            double l = jsonObject.getDouble("transactionAmount");
                            BigDecimal b = new BigDecimal(l);
                            entity.setTransactionAmount(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                        }
                        if (!jsonObject.isNull("transactionCharge")) {
                            entity.setTransactionCharge(jsonObject.getString("transactionCharge"));
                        }
                        if (!jsonObject.isNull("transactionDate")) {
                            entity.setTransactionDate(jsonObject.getString("transactionDate"));
                        }
                        if (!jsonObject.isNull("transactionRate")) {
                            entity.setTransactionRate(jsonObject.getString("transactionRate"));
                        }
                        if (!jsonObject.isNull("transactionStatus")) {
                            entity.setTransactionStatus(jsonObject.getString("transactionStatus"));
                        }
                        if (!jsonObject.isNull("transactionType")) {
                            entity.setTransactionType(jsonObject.getString("transactionType"));
                        }
                        if (!jsonObject.isNull("transactionUnit")) {
                            entity.setTransactionUnit(jsonObject.getString("transactionUnit"));
                        }
                        list.add(entity);
                    }
                }
                msg.setData(list);
            }
        }
        return msg;
    }
}
